package com.sugon.controller.tool;

import com.sugon.annotation.AutoLog;
import com.sugon.entity.ToolsStockEntity;
import com.sugon.entity.ToolsStorePosEntity;
import com.sugon.service.ToolsStockService;
import com.sugon.service.ToolsStorePosService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 工装库存Controller
 *
 * @author sugon
 * @date 2019-08-03 16:20:29
 */
@Controller
@RequestMapping("toolsstock")
public class ToolsStockController {
    @Autowired
    private ToolsStockService toolsStockService;
    @Autowired
    private ToolsStorePosService toolsStorePosService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("toolsstock:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {

        //查询列表数据
        Query query = new Query(params);

        List<ToolsStockEntity> toolsStockList = toolsStockService.queryList(query);
        int total = toolsStockService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(toolsStockList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("toolsstock:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ToolsStockEntity toolsStock = toolsStockService.queryObject(id);

        return R.ok().put("toolsStock", toolsStock);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("toolsstock:save")
    @ResponseBody
    public R save(@RequestBody ToolsStockEntity toolsStock) {
        int y = toolsStockService.queryValid(toolsStock);
        Boolean isExists = false;
        if (y != 0) {
            isExists = true;
            return R.error(201, "已存在，请勿重复添加。");
        }
        toolsStockService.save(toolsStock);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("toolsstock:update")
    @ResponseBody
    public R update(@RequestBody ToolsStockEntity toolsStock) {
        toolsStockService.update(toolsStock);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("toolsstock:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        toolsStockService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ToolsStockEntity> list = toolsStockService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 根据系统编码查询数据
     * @param erpCode
     * @return
     */
    @RequestMapping("/queryByCode/{erpCode}")
    @ResponseBody
    public R queryByCode(@PathVariable("erpCode") String erpCode) {

        ToolsStockEntity toolsStock = toolsStockService.queryByCode(erpCode);

        return R.ok().put("toolsStock", toolsStock);
    }


    /**
     * 查看列表
     */
    @RequestMapping("/listToolsPos")
    @ResponseBody
    public R listSparePos(@RequestParam Map<String, Object> params) {
        params.put("delFlag", 1);
        //查询列表数据
        Query query = new Query(params);
        List<ToolsStorePosEntity> posList = toolsStorePosService.queryList(query);
        int total = toolsStorePosService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(posList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }
}
